package pattern.behavior.observer.spring;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
 * @author : Rookie
 * @description :
 * @date : 2023/8/26
 */
@Component
@Slf4j
public class EventPublisherService {

    private final ApplicationEventPublisher eventPublisher;

    public EventPublisherService(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    public void publishCustomEvent(Object source) {
        CustomEvent customEvent = new CustomEvent(source);
        log.info("发送消息：{}", JSONUtil.toJsonStr(source));
        eventPublisher.publishEvent(customEvent);
    }
}
